<?php

App::import('Model', 'Expense');

class RefinancingPlansController extends AppController {

    var $name = 'RefinancingPlans';

	/* De acuerdo a las propiedades que puede ver, agarro sus expensas
		* y dentro de ellas las que tiene configurado un planId*/
	function get_plan_id ()
	{
		$expense = new Expense;
 		$permit  = $this->Session->read('permit_property');
        if ($permit != null){
        	$data = null;    
            $new  = null;    
            foreach ($permit as $key => $value) {
				$property_id = $value["property_id"];
		    	$sql = "select refinancing_plan_id 
						from expenses 
						where  refinancing_plan_id is not null"; //le saco  "property_id = $property_id and"; porque no mostraba todos los planes
				$data[$key] = $expense->query($sql);        
			}
			foreach ( $data as $key1 => $value1)
			{  
				foreach ( $value1 as $key2 => $value2)
				{ 
					$new[$key2] = $value2["expenses"]["refinancing_plan_id"];
				}  
			} 
		}
		return $new;
	}

    function index() {
        $this->RefinancingPlan->recursive = 0;
/*		$permit = $this->get_plan_id();
		if ($permit != null){
                $data = null;    
                $new  = null;  
                foreach ($permit as $key => $value) {
                    $this->paginate = array(
                                 'conditions' => array( 'RefinancingPlan.refinancing_plan_id' => $value )
                             );  
                    $data[$key]  = $this->paginate();
                }   
                foreach ( $data as $key1 => $value1)
                {   
                    if ( $value1 != null ) 
						$new[$key1] = $value1[0];
                }   
                    $this->set('refinancingPlans', $new);
            }else{
              $this->Session->setFlash(__('You have nothing assigned', true));
              $this->redirect(array('controller' => 'users', 'action' => 'welcome'));
            }   
 */  
          $this->set('refinancingPlans', $this->paginate());
	}

    function view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid refinancing plan', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('refinancingPlan', $this->RefinancingPlan->read(null, $id));
    }

    function add() {
        if (!empty($this->data)) {
            $this->RefinancingPlan->create();
            if ($this->RefinancingPlan->save($this->data)) {
                $this->Session->setFlash(__('The refinancing plan has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The refinancing plan could not be saved. Please, try again.', true));
            }
        }
        $people = $this->RefinancingPlan->Person->find('list');
        $this->set(compact('people'));
    }

    function edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid refinancing plan', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->RefinancingPlan->save($this->data)) {
                $this->Session->setFlash(__('The refinancing plan has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The refinancing plan could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->RefinancingPlan->read(null, $id);
        }
        $people = $this->RefinancingPlan->Person->find('list');
        $this->set(compact('people'));
    }

    function delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for refinancing plan', true));
            $this->redirect(array('action' => 'index'));
        }
        if ($this->RefinancingPlan->delete($id)) {
            $this->Session->setFlash(__('Refinancing plan deleted', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Refinancing plan was not deleted', true));
        $this->redirect(array('action' => 'index'));
    }

    function create_plan() {
        $this->RefinancingPlan->create_plan($this->data);
        /*if (!empty($this->data)) {
            if ($this->RefinancingPlan->create_plan($this->data)) {
                $this->Session->setFlash(__('The refinancing plan has been created', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The refinancing plan could not be created. Please, try again.', true));
            }
        }*/
        $this->redirect(array('action' => 'index'));
    }    
  
}

?>
